package cn.com.api.service.impl;

import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import cn.com.api.entity.ApiShop;
import cn.com.api.entity.ApiT_log;
import cn.com.api.entity.ApiT_products;
import cn.com.api.mapper.ApiShopsMapper;
import cn.com.api.mapper.ApiT_logMapper;
import cn.com.api.mapper.ApiT_productsMapper;
import cn.com.api.resultpage.InitShopListPage;
import cn.com.api.service.ApiT_logService;
import cn.com.api.service.ApiTypeService;
import cn.com.util.*;
import cn.com.util.*;

@Service
public class ApiT_logServiceImpl implements ApiT_logService {
	@Autowired
	private ApiTypeService apiTypeService;
	@Autowired
	private ApiT_productsMapper apiT_productsMapper;
	@Autowired
	private ApiT_logMapper apiT_logMapper;
	@Autowired
	private ApiShopsMapper apiShopsMapper;

	// 根据id获取
	public ApiT_log findT_logById(Integer c_id) {
		ApiT_log t_log = apiT_logMapper.findT_logById(c_id);
		return t_log;
	}

	// 添加
	public void insertT_log(ApiT_log t_log) {
		apiT_logMapper.insertT_log(t_log);
		return;
	}

	@Override
	public void addOrCancelLog(ApiT_log t_log, Map<String, Object> map) {
		// 判断是否已收藏，enable=1 收藏 enable=0 取消收藏
		ApiT_log tlog = apiT_logMapper.findTlogByTypeAndCuid(t_log);
		if (tlog != null) {
			// 判断是否已收藏
			Integer enable = tlog.getC_enable();
			if (1 == enable) {// 已收藏
				tlog.setC_enable(0);// 未收藏---收藏成功
				apiT_logMapper.updateT_log(tlog);
				map.put(Utils.RESULT, Utils.RESULT_RC_SUCCESS);
				map.put(Utils.MESSAGE, "取消收藏成功！");
			} else {
				tlog.setC_enable(1);// 已收藏--取消收藏成功
				apiT_logMapper.updateT_log(tlog);
				map.put(Utils.RESULT, Utils.RESULT_RC_SUCCESS);
				map.put(Utils.MESSAGE, "收藏成功！");
			}

		} else {
			t_log.setC_enable(1);// 添加收藏成功
			apiT_logMapper.insertT_log(t_log);
			map.put(Utils.RESULT, Utils.RESULT_RC_SUCCESS);
			map.put(Utils.MESSAGE, "收藏成功！");
		}
	}

	// 修改
	public void updateT_log(ApiT_log t_log) {
		apiT_logMapper.updateT_log(t_log);
		return;
	}

	// 删除
	public int deleteT_log(Integer c_id) {
		int ret_I = apiT_logMapper.deleteT_log(c_id);
		return ret_I;
	}

	// 取消收藏
	public int deleteT_logByUidUseridActType(Integer c_uid, String c_userid, String c_act, String c_type) {
		int ret_I = apiT_logMapper.deleteT_logByUidUseridActType(c_uid, c_userid, c_act, c_type);
		return ret_I;
	}

	// 我的收藏
	public List<ApiT_log> findT_logsByUserId(String c_userid) {
		List<ApiT_log> list = apiT_logMapper.findT_logsByUserId(c_userid);
		return list;
	}

	// 我收藏的店铺
	public List<ApiShop> findT_logs4ShopsByUserId_1(String c_userid, String cityid, Map<String, Object> map) {
		// List<Map<String, Object>> list = apiT_logMapper
		// .findT_logs4ShopsByUserId(c_userid,cityid);

		List<ApiShop> shopList = apiShopsMapper.findT_logs4ShopsByUserId(c_userid, cityid);
		if (shopList != null && shopList.size() > 0) {
			for (ApiShop shop : shopList) {
				shop.setTo_user_distance(0.00);
				shop.setIsEdit(apiTypeService.getShopType(shop.getcType()));
			}
		}
		InitShopListPage initShopListPage = new InitShopListPage();
		initShopListPage.setShopList(shopList);
		initShopListPage.setPage(new PageInfo());

		map.put(Utils.RESULT, Utils.RESULT_RC_SUCCESS);
		map.put(Utils.MESSAGE, "查询成功！");
		map.put("datas", initShopListPage);

		return shopList;
	}

	// 我收藏的商品
	public List<ApiT_products> findT_logs4GoodsByUserId_1(String c_userid, String cityid, Map<String, Object> map) {
		// List<Map<String, Object>> list = apiT_logMapper
		// .findT_logs4GoodsByUserId(c_userid,cityid);
		List<ApiT_products> goodsList = apiT_productsMapper.findT_logs4GoodsByUserId(c_userid, cityid);
		if (goodsList != null && goodsList.size() > 0) {
			for (ApiT_products product : goodsList) {
				Integer isEdit = product.getIsEdit();
				product.setIsEdit(apiTypeService.getShopType(isEdit));
			}
		}
		InitShopListPage initShopListPage = new InitShopListPage();
		initShopListPage.setProductsList(goodsList);
		initShopListPage.setPage(new PageInfo());
		map.put(Utils.RESULT, Utils.RESULT_RC_SUCCESS);
		map.put(Utils.MESSAGE, "查询成功！");
		map.put("datas", initShopListPage);
		return goodsList;
	}

	@Override
	public List<Map<String, Object>> findT_logs4GoodsByUserId(String c_userid, Map<String, Object> map) {
		List<Map<String, Object>> list = apiT_logMapper.findT_logs4GoodsByUserId(c_userid);
		map.put(Utils.RESULT, Utils.RESULT_RC_SUCCESS);
		map.put(Utils.MESSAGE, "查询成功！");
		map.put("datas", list);
		return list;
	}

	@Override
	public List<Map<String, Object>> findT_logs4ShopsByUserId(String c_userid, String cityid, Map<String, Object> map) {
		List<Map<String, Object>> list = apiT_logMapper.findT_logs4ShopsByUserId(c_userid, cityid);
		map.put(Utils.RESULT, Utils.RESULT_RC_SUCCESS);
		map.put(Utils.MESSAGE, "查询成功！");
		map.put("datas", list);
		return list;
	}

	@Override
	public boolean isFollow(String cuserid, String shop_id, String c_act) {
		ApiT_log apiT_log = apiT_logMapper.isFollow(cuserid, shop_id, c_act);
		if (apiT_log == null) {
			return false;
		} else {
			return true;
		}
	}
}
